const {app,router} = require('./lib/system.js')
const Pool = require('./lib/database')
const path = require('path')
const staticFiles = require('koa-static')

// 新闻
const article = require('./backstage/article/index')
router.use('/article',article.routes());
// 图片
const image = require('./backstage/image/index');
router.use('/image',image.routes())

// 全局参数
app.use(async (ctx, next) => {
    ctx.state = {
        url: '123'
    }
    ctx.pool = new Pool();
    ctx.success = function(d){
        return ctx.body = {
            code:200,
            msg:'success',
            data:d
        }
    }
    ctx.fail = function(d){
        return ctx.body = {
            code:'200',
            msg:'fail',
            data:d
        }
    }
    await next();
})

router.get('/', function (ctx, next) {
    ctx.body="Hello koa";
})
router.get('/index', function(ctx, next) {
    let url = ctx.url;
    //从request中获取GET请求
    let request = ctx.request;
    let req_query = request.query;
    let req_querystring = request.querystring;
    //从上下文中直接获取
    let ctx_query = ctx.query;
    let ctx_querystring = ctx.querystring;
    ctx.body = {
        url,
        req_query,
        req_querystring,
        ctx_query,
        ctx_querystring,
        
    }
})
// 静态资源地址，主要是图片
app.use(staticFiles(path.resolve(__dirname, "./public")))

app.use(router.routes());
app.use(router.allowedMethods())
app.listen(3000, () => {
    console.log('good!', 'http://localhost:3000')
    
})